package com.excel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.htmlparser.tags.TableColumn;
/**
 * 导入导出的中间结构
 * @author Administrator
 *
 */
public 	class TableModel {
	private int colLen = 0;
	private int rowLen = 0;
	public CELL[][] cells;
	
	
	
	//TODO
	public int rowIndex = -1;
	public int colIndex = -1;
	public int rows = -1;
	public int cols = -1;
	public String name = "";
	
	public String description;


	private String runsParam;

	public int pmbNo;
	

	private String category;
	////////////////////////////////
	
	//标记属性之用
	public Map<String, List<CELL>> aMap = new HashMap<String, List<CELL>>();
	
	public void putAtr(String name, CELL aCELL) {
		if(name!= null && !name.equals("")) {
			List<CELL> aList = aMap.get(name);
			if(aList != null) {
				aList.add(aCELL);
			}else {
				aList = new ArrayList<CELL>();
				aList.add(aCELL);
				aMap.put(name, aList);
			}
		}
	}
	
	public String toString() {
		StringBuffer sbf = new StringBuffer();
		for (int i = 0; i < rowLen; i++) {
			for (int j = 0; j < colLen; j++) {
				if(j==(0)) {
					sbf.append("\r\n");
				}
				sbf.append("\t"+this.cells[i][j].toString());
			}
		}
		return sbf.toString();
	}
	
	
	public TableModel(int rowLen, int colLen) {
		super();
		this.colLen = colLen;
		this.rowLen = rowLen;
		this.cells = new CELL[rowLen][colLen];
		for (int i = 0; i < rowLen; i++) {
			for (int j = 0; j < colLen; j++) {
				this.cells[i][j] = new CELL();
			}
		}
	}

	/**
	 * 给定rowIndex,将单元格放入cells
	 */
	public void setCell(int rowIndex, TableColumn tdElement) {
		String colsSpanStr = tdElement.getAttribute("colspan");
		String rowSpanStr = tdElement.getAttribute("rowspan");
		int colSpan = colsSpanStr == null ? 1 : Integer
				.parseInt(colsSpanStr);
		int rowSpan = rowSpanStr == null ? 1 : Integer.parseInt(rowSpanStr);
		CELL[] row = this.cells[rowIndex];
		for (int i = 0; i < row.length; i++) {
			CELL cell = row[i];
			// 见缝插针
			if (cell.cellPosition == null) {
				fillCell(rowIndex, i, rowSpan, colSpan, tdElement);

				break;
			}
		}
	}

	private void fillCell(int rowIndex, int colIndex, int rowSpan,
			int colspan, TableColumn tdElement) {
		for (int i = 0; i < rowSpan; i++) {
			for (int j = 0; j < colspan; j++) {
				CELL cell = this.cells[rowIndex + i][colIndex + j];
				cell.cellPosition = "" + (rowIndex) + "_" + (colIndex);
				cell.aTD = tdElement;
				// it's importnt , cause it may involved with data import

			}
		}
		tdElement.setAttribute("rowIndex", "" + (rowIndex));
		tdElement.setAttribute("colIndex", "" + (colIndex));
	}
	
	/**
	 * 
	 * @param rowIndex 二维表中的位置
	 * @param colIndex 二维表中的位置
	 * @param left 代表哪个单元格
	 * @param top 代表哪个单元格
	 * @param tdElement
	 * @param blank
	 */
	public void fillCell(int rowIndex, int colIndex, int top,
			int left, TableColumn tdElement, boolean blank) {
		
				CELL cell = this.cells[rowIndex][colIndex];
				cell.cellPosition = "" + (top) + "_" + (left);
				cell.aTD = tdElement;
				// it's importnt , cause it may involved with data import

		tdElement.setAttribute("rowIndex", "" + (top));
		tdElement.setAttribute("colIndex", "" + (left));
	}

}